package com.javasm.service.B2cOrderService.impl;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.orderCriteria.OrderCriteria;
import com.javasm.domin.criteria.orderCriteria.OrderProductCriteria;
import com.javasm.domin.entity.orderEntity.B2cOrder;
import com.javasm.domin.entity.orderEntity.B2cReturnGoods;
import com.javasm.domin.vo.orderVo.OrderAndReceiver;
import com.javasm.domin.vo.orderVo.OrderExcelVo;
import com.javasm.domin.vo.orderVo.OrderVo;
import com.javasm.domin.vo.orderVo.ProductVo;
import com.javasm.mapper.B2cOrderMapper.OrderMapper;
import com.javasm.mapper.B2cOrderMapper.ReturnGoodsMapper;
import com.javasm.service.B2cOrderService.OrderService;
import com.javasm.transfer.orderTransfer.OrderTransfer;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lys
 * @since 2021-11-18
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, B2cOrder> implements OrderService {

    private final OrderMapper orderMapper;
    private final ReturnGoodsMapper returnGoodsMapper;
    private final OrderTransfer orderTransfer;

    @Override
    public List<OrderAndReceiver> findList() {
        return orderMapper.findList();
    }


    /**
     * 条件查询
     *
     * @param orderCriteria
     * @return
     */
    @Override
    public PageResult<OrderAndReceiver> search(OrderCriteria orderCriteria) {
        PageHelper.startPage(orderCriteria.getCurrentPage(), orderCriteria.getPageSize());
        LambdaQueryWrapper<B2cOrder> lambda = new QueryWrapper<B2cOrder>().lambda();
        if (!StringUtils.isEmpty(orderCriteria.getId())) {
            lambda.eq(B2cOrder::getId, orderCriteria.getId());
        }
        if (!StringUtils.isEmpty(orderCriteria.getOrderAction())) {
            lambda.eq(B2cOrder::getOrderAction, orderCriteria.getOrderAction());
        }
        if (!StringUtils.isEmpty(orderCriteria.getOrderType())) {
            lambda.eq(B2cOrder::getOrderType, orderCriteria.getOrderType());
        }
        if (!StringUtils.isEmpty(orderCriteria.getOrderStatus())) {
            lambda.eq(B2cOrder::getOrderStatus, orderCriteria.getOrderStatus());
        }
        if (!StringUtils.isEmpty(orderCriteria.getBusinessType())) {
            lambda.eq(B2cOrder::getBusinessType, orderCriteria.getBusinessType());
        }
        if (!StringUtils.isEmpty(orderCriteria.getPaymentMethodId())) {
            lambda.eq(B2cOrder::getPaymentMethodId, orderCriteria.getPaymentMethodId());
        }
        if (!StringUtils.isEmpty(orderCriteria.getStartTime())) {
            lambda.between(B2cOrder::getCreateTime, orderCriteria.getStartTime(),orderCriteria.getEndTime());
        }
        if (!StringUtils.isEmpty(orderCriteria.getOrderStatusList())){
            lambda.in(B2cOrder::getOrderStatus,orderCriteria.getOrderStatusList());
        }
        if (!StringUtils.isEmpty(orderCriteria.getOrderTypeList())){
            lambda.in(B2cOrder::getOrderType,orderCriteria.getOrderTypeList());
        }
        List<B2cOrder> list = orderMapper.selectList(lambda);
        PageInfo<B2cOrder> pageInfo = new PageInfo<>(list);
        //转换vo
        List<OrderAndReceiver> andReceiverList = orderTransfer.Escape(list);
        //返回分页结果
        return new PageResult<OrderAndReceiver>(pageInfo.getTotal(), andReceiverList);
    }

    /**
     * 批量查询
     *
     * @param idList
     * @return
     */
    @Override
    public List<OrderAndReceiver> findByIdList(List<Serializable> idList) {
        List<B2cOrder> b2cOrders = orderMapper.selectBatchIds(idList);
        //转换vo
        List<OrderAndReceiver> andReceiverList = orderTransfer.Escape(b2cOrders);
        return andReceiverList;
    }



    @Override
    public void excelExport(HttpServletResponse response) {
//        FileOutputStream fileOutputStream = null;
//        List<B2cOrder> b2cOrders = orderMapper.selectList(null);
//        String fileName = "订单表";
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
//        response.setCharacterEncoding("utf-8");
//        response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xls");
//        ServletOutputStream out = null;
//        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLS,true);
//        try {
//            out = response.getOutputStream();
//            SXSSFWorkbook workbook = new SXSSFWorkbook(100); //每100个row自动保存到硬盘
//            Sheet sheet = workbook.createSheet("订单信息");
//            writer.finish();
//            out.flush();
//            response.getOutputStream().close();
//            out.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }


    @Override
    public List<B2cOrder> findIdList(List<Serializable> idList) {
        List<B2cOrder> b2cOrderList = orderMapper.selectBatchIds(idList);
        return b2cOrderList;
    }

    /**
     * 退货
     *
     * @param orderCriteria
     * @return
     */
    public PageResult<OrderAndReceiver> find(OrderCriteria orderCriteria) {
        PageHelper.startPage(orderCriteria.getCurrentPage(), orderCriteria.getPageSize());
        LambdaQueryWrapper<B2cReturnGoods> lambda = new QueryWrapper<B2cReturnGoods>().lambda();
        if (!StringUtils.isEmpty(orderCriteria.getRejectReason())) {
            lambda.like(B2cReturnGoods::getRejectReason, orderCriteria.getRejectReason());
        }
        if (!StringUtils.isEmpty(orderCriteria.getRejectTime())) {
            lambda.like(B2cReturnGoods::getRejectTime, orderCriteria.getRejectTime());
        }
        List<B2cReturnGoods> list = returnGoodsMapper.selectList(lambda);

        PageInfo<B2cReturnGoods> pageInfo = new PageInfo<>(list);
        //返回分页结果
        return null;
    }


    /**
     * 修改订单状态
     * @param orderId
     * @return
     */
    @Override
    public Integer updateOrderStatus(Serializable orderId) {
        B2cOrder b2cOrder = new B2cOrder();
        int i = orderMapper.updateById(b2cOrder);
        return null;
    }

    @Override
    public OrderAndReceiver findOrderInfo(Serializable id) {
        B2cOrder b2cOrder = orderMapper.selectById(id);
        OrderAndReceiver orderAndReceiver = orderTransfer.setString(b2cOrder);
        return orderAndReceiver ;
    }
}
